
#define GPBCON     ((volatile unsigned long*)0x56000010)
#define GPBDATA    ((volatile unsigned long*)0x56000014)

#define LED1_MASK  (1 << 5)
#define LED2_MASK  (1 << 6)
#define LED3_MASK  (1 << 7)
#define LED4_MASK  (1 << 8)

void wait(volatile unsigned long cnt) {
    for(;cnt > 0; --cnt);
}

void set_led(int led_no, int is_set_on) {
    switch(led_no) {
        case 1:
            if (1 == is_set_on) {
                *GPBDATA &= ~LED1_MASK; //set bit5 to 0, to let led1 on
            } else {
                *GPBDATA |= LED1_MASK; //set bit5 to 1, to let led1 off
            }
            break;
        case 2:
            if (1 == is_set_on) {
                *GPBDATA &= ~LED2_MASK; //set bit6 to 0, to let led2 on
            } else {
                *GPBDATA |= LED2_MASK; //set bit6 to 1, to let led2 off
            }
            break;
        case 3:
            if (1 == is_set_on) {
                *GPBDATA &= ~LED3_MASK; //set bit7 to 0, to let led3 on
            } else {
                *GPBDATA |= LED3_MASK; //set bit7 to 1, to let led3 off
            }
            break;
        case 4:
            if (1 == is_set_on) {
                *GPBDATA &= ~LED4_MASK; //set bit8 to 0, to let led4 on
            } else {
                *GPBDATA |= LED4_MASK; //set bit8 to 1, to let led4 off
            }
            break;
        default:
            if (1 == is_set_on) {
                *GPBDATA &= ~LED1_MASK; //set bit5 to 0, to let led1 on
                *GPBDATA &= ~LED2_MASK; //set bit5 to 0, to let led1 on
                *GPBDATA &= ~LED3_MASK; //set bit5 to 0, to let led1 on
                *GPBDATA &= ~LED4_MASK; //set bit5 to 0, to let led1 on
            } else {
                *GPBDATA |= LED1_MASK; //set bit5 to 1, to let led1 off
                *GPBDATA |= LED2_MASK; //set bit5 to 1, to let led1 off
                *GPBDATA |= LED3_MASK; //set bit5 to 1, to let led1 off
                *GPBDATA |= LED4_MASK; //set bit5 to 1, to let led1 off
            }
            break;
    }
}

char led_control[5][2] = {{1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}};

int main() {
    *GPBCON |= (0x55 << 10); //set GPB5(led1), GPB6(led2), GPB7(led3), GPB8(led4) as output port
    *GPBDATA = 0x0;
    char i = 0;
    char led_idx = 0;
    char onoff = 0;

    while(1) {
        wait(30000);
        led_idx = i / 2; //0-4
        onoff = i % 2; //0-1

        set_led(led_idx, onoff);

        ++i;
        if (i > 9) i = 0; //0-9
    }
    return 0;
}
